package org.acegisecurity.util.expressioninterpreter;

import java.util.ArrayList;
import java.util.EmptyStackException;

/**
 * a non Synchronized Stack implementation.
 * This stack implements a last-in-first-out (LIFO) stack of objects.
 *
 * @author Ivo Woltring
 * @author Jamie Craane
 */
public class Stack extends ArrayList {
    private static final long serialVersionUID = 3840996112324209347L;

    /**
     * Adds an element to the queue
     *
     * @param element the element to push on the queue
     */
    public void push(Object element) {
        super.add(element);
    }

    /**
     * returns the first object pushed to the queue (FIFO)
     *
     * @return token the token to return
     */
    public Object pop() {
        if (super.size() <= 0) {
            throw new EmptyStackException();
        }
        Object element = super.get(this.size() - 1);
        super.remove(this.size() - 1);
        return element;
    }

    /**
     * Peek from the Queue
     *
     * @return token the look ahead token
     */
    public Object peek() {
        if (super.size() <= 0) {
            throw new EmptyStackException();
        }
        return super.get(this.size() - 1);
    }

    /**
     * returns true if the queue is empty and false if not
     *
     * @return boolean if the stack is empty
     */
    public boolean empty() {
        return super.size() == 0;
    }

}
